<?php

namespace App\Http\Controllers\H5;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\CustomerAuth;
use App\Params\H5\CartAddParams;
use App\Params\H5\CartCheckoutParams;
use App\Params\H5\CartUpdateParams;
use App\Services\Cart\CartsService;
use App\Traits\VerifyRequestParam;
use Illuminate\Http\Request;

/**
 * 购物车相关控制器
 *
 * @author WangBin
 * @date 2021-09-17
 * @package App\Http\Controllers\H5
 */
class CartsController extends BaseController
{
    use VerifyRequestParam;

    public function __construct()
    {
        // 校验token中间件
        $this->middleware(CustomerAuth::class);
    }

    /**
     * 添加购物车商品
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function add()
    {
        $param = CartAddParams::create();
        $total = CartsService::getInstance()->add($param);
        return $this->success(['total' => $total]);
    }

    /**
     * 统计购物车商品总数
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function total()
    {
        $customerId = $this->verifyInteger('customer_id');
        $total = CartsService::getInstance()->getTotal($customerId);
        return $this->success(['total' => $total]);
    }

    /**
     * 获取客户购物车
     *
     * @throws
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList()
    {
        $customerId = $this->verifyInteger('customer_id');
        $ret = CartsService::getInstance()->getCartItems($customerId);
        return $this->success($ret);
    }

    /**
     * 更新购物车
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function update()
    {
        $param = CartUpdateParams::create();
        $total = CartsService::getInstance()->update($param);
        return $this->success(['total' => $total]);
    }

    /**
     * 结算页数据
     *
     * @throws
     * @return \Illuminate\Http\JsonResponse
     */
    public function checkout()
    {
        $param = CartCheckoutParams::create();
        $ret = CartsService::getInstance()->checkout($param);
        return $this->success($ret);
    }

    /**
     * 获取客户购物车收货地址
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAddress(Request $request)
    {
        $customerId = $request->attributes->get('customer_id');
        $fastBuy = $request->input('fast_buy', 0);
        $ret = CartsService::getInstance()->getAddress($customerId, $fastBuy);
        return $this->success($ret);
    }

    /**
     * 删除购物车商品
     *
     * @return mixed
     * @throws \App\Exceptions\BusinessException
     */
    public function delete()
    {
        $productIds = $this->verifyArray('product_ids');
        $customerId = $this->verifyInteger('customer_id');
        CartsService::getInstance()->delete($productIds, $customerId);
        return $this->success();
    }
}
